package com.sumainfor.zuul.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sumainfor.common.util.CustomMap;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.util.ObjectUtil;
import com.sumainfor.common.util.RRException;
import com.sumainfor.zuul.dao.SysCompanyMapper;
import com.sumainfor.zuul.entity.SysCompanyEntity;
import com.sumainfor.zuul.entity.SysUserEntity;
import com.sumainfor.zuul.service.SysCompanyService;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections.MapUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Jack Chen
 * @Create 2019-01-22 10:06
 */
@Service
@Log4j2
@Transactional(rollbackFor = Exception.class)
public class SysCompanyServiceImpl extends ServiceImpl<SysCompanyMapper, SysCompanyEntity>
        implements SysCompanyService {


    @Override
    public JsonResult getInfoById(Integer id) {
        SysCompanyEntity entity = this.selectById(id);
        if (entity == null) {
            return JsonResult.actionFailure();
        }
        CustomMap data = ObjectUtil.convertObjectToMap(entity);

        //获得归属区域（公司）-线下转账	信息数据
        List<Map<String, Object>> list = baseMapper.selectAreaAccount(entity.getCompId());

        Map<String, Object> account = new HashMap<>(6);
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> map = list.get(i);
            String col;
            if (MapUtils.getInteger(map, "AccType") == 0) {
                col = "fz";
            } else {
                col = "sd";
            }
            if (MapUtils.getInteger(map, "PayType") == 1) {
                col += "Wx";
            } else if (MapUtils.getInteger(map, "PayType") == 2) {
                col += "AliPay";
            } else if (MapUtils.getInteger(map, "PayType") == 3) {
                col += "Card";
            }
            account.put(col, map.get("Account"));
        }

        data.put("PayAccount", account);

        return JsonResult.success(data);
    }


    ///////////////////////////////////////////////////////////////////////////
    // 转账信息
    ///////////////////////////////////////////////////////////////////////////

    /**
     * 插入 归属区域（公司）-线下转账
     *
     * @param list
     * @param areaId
     * @return
     */
    @Override
    public boolean insertPayData(List<Map<String, Object>> list, Integer areaId) {
        SysUserEntity entity = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> map = list.get(i);
            int state = baseMapper.insertAreaAccountData(CustomMap.create()
                    .put("CompId", areaId)
                    .put("AccType", map.get("AccType"))
                    .put("PayType", map.get("PayType"))
                    .put("Account", map.get("Account"))
                    .put("delfg", 0)
                    .put("instm", new Date())
                    .put("insby", entity.getUserId())
            );
            if (state <= 0) {
                throw new RRException("插入数据出错");
            }
        }
        return true;
    }

    @Override
    public boolean deleteAreaAccountAllByAreaId(Integer comId) {
        try {
            baseMapper.deleteAreaAccountData(comId);
        } catch (Exception e) {
            log.error("删除数据错误:", e);
            return false;
        }
        return true;
    }

    @Override
    public boolean updateAreaAccountByAreaId(List<Map<String, Object>> list, Integer comId) {
        boolean state = deleteAreaAccountAllByAreaId(comId);
        if (state == false) {
            return false;
        }
        return this.insertPayData(list, comId);
    }

}
